package com.helei.mq.service.Impl;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.helei.mq.config.RabbitMqConstant;
import com.helei.mq.service.TestService;
import com.helei.mq.util.RabbitMqUtil;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.Type;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;

/**
 * @ Tool：IntelliJ IDEA
 * @ Author：hl
 * @ Date：2024-05-06-下午5:36
 */
@Service
public class TestServiceImpl implements TestService {
    @Autowired
    private RabbitMqUtil rabbitMqUtil;
    @Override
    public void send() {
        HashMap<Object, Object> socketData = new HashMap<>();
        socketData.put("message","何磊");
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");
        socketData.put("date",sdf1.format(new Date()));
        rabbitMqUtil.sendMessage(RabbitMqConstant.EXCHANGE_TEST, RabbitMqConstant.ROUTING_TEST, new Gson().toJson(socketData),2000);
    }
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(RabbitMqConstant.QUEUE_TEST),
            key = RabbitMqConstant.ROUTING_TEST,
            exchange = @Exchange(value=RabbitMqConstant.EXCHANGE_TEST, type = RabbitMqConstant.DELAY_TYPE)
    ))
    public void responseToUser(String content) {
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");
        String format = sdf1.format(new Date());
        System.out.println("接受到消息时间----"+format);
        HashMap<Object, Object> socketData = new Gson().fromJson(content, new TypeToken<HashMap<Object, Object>>() {
        }.getType());
        String date = String.valueOf(socketData.get("date"));
        System.out.println("发送消息时间----"+date);
        System.out.println("消息----"+content);
    }
}
